﻿using com.uplus.common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApi.Bill.Biz;
using WebApi.Models;

namespace WebApi.Controllers
{
    public class CommentInfoController : BaseController
    {

        #region 新增意见反馈 Feedback
        /// <summary>
        /// 新增意见反馈  add by cfl  2022-04-14
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<CommentInfoModel> Feedback([FromBody] dynamic param)
        {
            ResponseDataMsg res = new ResponseDataMsg();

            try
            {
                FeedbackPostData postData = JsonConvert.DeserializeObject<FeedbackPostData>(JsonConvert.SerializeObject(param));
                var accessToken = Request.Headers["Authorization"];
                DateTime utcNow = DateTime.UtcNow;
                string key = AppConfigurtaion.GetSecondSection("JwtSetting", "SecurityKey"); //"d0ecd23c-dfdb-4005-a2ea-0fea210c858a";
                string jwtIssuerUplus = AppConfigurtaion.GetSecondSection("JwtSetting", "Issuer");
                string jwtAudienceUplus = AppConfigurtaion.GetSecondSection("JwtSetting", "Audience");
                SecurityKey securityKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(key));
                //校验token
                var validateParameter = new TokenValidationParameters()
                {
                    ValidateLifetime = true,
                    ValidateAudience = true,
                    ValidateIssuer = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = jwtIssuerUplus,
                    ValidAudience = jwtAudienceUplus,
                    IssuerSigningKey = securityKey,
                };

                //校验并解析token
                var claimsPrincipal = new JwtSecurityTokenHandler().ValidateToken(accessToken, validateParameter, out SecurityToken validatedToken);//validatedToken:解密后的对象
                var jwtPayload = ((JwtSecurityToken)validatedToken).Payload.SerializeToJson(); //获取payload中的数据 
                dynamic dataJson = JsonConvert.DeserializeObject<dynamic>(jwtPayload);
                string UserId = dataJson["UserID"];
                UserModel data = BizBase.Get<BizUser>().Get(UserId);
                if (data != null)
                {
                    CommentInfoModel comment = new CommentInfoModel();
                    comment.userComment = postData?.content;
                    comment.feedback = postData?.title;
                    comment.userID = Convert.ToInt32(UserId);
                    BizBase.Get<BizCommentInfo>().Add(comment);
                    res.code = 200;
                    res.msg = "操作成功";
                }
                else
                {
                    res.code = 201;
                    res.msg = "用户不存在";
                }

                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                return this.ControllerExHandler(res, "Feedback", ex);
            }
        }
        #endregion

    }
}
